package com.mymail.mailserviceimpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Time;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import com.mymail.mailservice.MailService;
import com.mymail.model.Order;
@Component(value="emailservice")
public class SendOrderConfirmationEmailAdvice implements MailService {
	
	
	//private String from = "marco.freeman@gmail.com";
	//private String TemplateName = "com/mymail/common/mail_temp.txt";
	//private MailSender mailSender;
	private JavaMailSender javaMailSender;
	//private Order order;
	
	
	
	
	
//	@Resource
//	public void setMailSender(MailSender mailSender) {
//		this.mailSender = mailSender;
//	}

	
	@Resource
	public void setJavaMailSender(JavaMailSender javaMailSender) {
		this.javaMailSender = javaMailSender;
	}




	@Override
	public void sendOrderMail(Order order) {
//		SimpleMailMessage mailMessage = new SimpleMailMessage();
//		mailMessage.setFrom(from);
//		mailMessage.setTo(order.getUsermail());
//		mailMessage.setSubject("��ϲ��ɹ�ע��");
//		mailMessage.setText("sdasjdakljg");
//		mailSender.send(mailMessage);
		
		
		MimeMessage message = javaMailSender.createMimeMessage();
		Time time = new Time(System.currentTimeMillis());
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message);
			helper.setFrom("11148509@qq.com");
			helper.setTo(order.getUsermail());
			helper.setSubject("sss");
			helper.setText("this is a test,and now is"+time);
			//FileSystemResource file = new FileSystemResource(new File("d:/logo.ai"));
			//helper.addAttachment("logo.ai", file);  ��������Ϊ��Ӹ���
			javaMailSender.send(message);
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}


//	private String loadTemplateContent(String templateName) {
//		StringBuilder sb = new StringBuilder();
//		BufferedReader br = null;
//		try {
//			br = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(templateName),"UTF-8"));
//			String lineSeparator = System.getProperty("line.separator"); 
//			for (String str = null; (str = br.readLine()) != null;) {
//				sb.append(str);
//				sb.append(lineSeparator);
//			}
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//		finally{
//			if(br!=null){
//				try{
//					br.close();
//				}
//				catch (IOException e3) {
//					// TODO: handle exception
//					e3.printStackTrace();
//				}
//			}
//		}
//		return br.toString();
//	}

}
